package com.hot12345.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hot12345.common.basedata.Constant;
import com.hot12345.dao.InfoDaoImpl;
import com.hot12345.entity.Click;
import com.hot12345.entity.Info;
import com.hot12345.vo.InfoVO;

public class InfoServiceImpl {
	
	private final static Log log = LogFactory.getLog(InfoServiceImpl.class);

	private final static InfoServiceImpl instance = new InfoServiceImpl();
	private InfoServiceImpl(){}
	
	public static InfoServiceImpl getInstance(){
		return instance;
	}
	
	/**
	 * 根据类型获取最热门的排行信息。
	 *	
	 * @param dateType 数据类型，如当天，一星期，一个月等。@see Constant.dateType_day
	 * @param itemID 栏目ID
	 * @param startNo
	 * @param pageSize
	 * @return
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-11 下午07:16:20
	 * @version V1.0
	 */
	public List<InfoVO> getInfoList(int dateType, Integer itemID, int startNo, int pageSize) throws Exception{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("state", 1);
		params.put("itemID", itemID);
		params.put("parentID", 0);
		
		if(dateType==Constant.dateType_day){
			//当天
			ClickServiceImpl clickSer = ClickServiceImpl.getInstance();
			List<Click> clickList = clickSer.getClickList(Constant.dateType_day, itemID, startNo, pageSize);
			
			return getInfoByClickList(clickList);
		} else if(dateType==Constant.dateType_24Hour){
			//24小时
			ClickServiceImpl clickSer = ClickServiceImpl.getInstance();
			List<Click> clickList = clickSer.getClickList(Constant.dateType_24Hour, itemID, startNo, pageSize);
			
			return getInfoByClickList(clickList);
		} else if(dateType==Constant.dateType_week){
			//一周			
			ClickServiceImpl clickSer = ClickServiceImpl.getInstance();
			List<Click> clickList = clickSer.getClickList(Constant.dateType_week, itemID, startNo, pageSize);
			
			return getInfoByClickList(clickList);			
		} else if(dateType==Constant.dateType_month){
			//一月
			ClickServiceImpl clickSer = ClickServiceImpl.getInstance();
			List<Click> clickList = clickSer.getClickList(Constant.dateType_month, itemID, startNo, pageSize);
			return getInfoByClickList(clickList);
		} else if(dateType==Constant.dateType_6month){
			//半年
			ClickServiceImpl clickSer = ClickServiceImpl.getInstance();
			List<Click> clickList = clickSer.getClickList(Constant.dateType_6month, itemID, startNo, pageSize);
			return getInfoByClickList(clickList);
		} else {
			//最新直接信息表
//			ClickServiceImpl clickSer = ClickServiceImpl.getInstance();
//			List<Click> clickList = clickSer.getClickList(Constant.dateType_last, itemID, startNo, pageSize);
//			return getInfoByClickList(clickList);
			return getInfoList(itemID, startNo, pageSize);
		}	
	}
	
	public List<InfoVO> getInfoByClickList(List<Click> clickList) throws Exception{
		if(clickList==null || clickList.size()==0){
			return null;
		}
		if(clickList!=null && clickList.size()>0){
			List<Long> idList = new LinkedList<Long>();
			for(Click click : clickList){
				idList.add(click.getInfoID());
			}
			
			Map<String,Object> params1 = new HashMap<String,Object>();
			params1.put("idList", idList);
			InfoDaoImpl infoDao = InfoDaoImpl.getInstance();
			List<InfoVO> topList = infoDao.findByIds(params1);
			return topList;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<InfoVO> getInfoList(Integer itemID, int startNo, int pageSize) throws Exception{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("itemID", itemID);
		params.put("parentID", 0);//同一组只取一条。
		params.put("state", 1);
		params.put("order", "isTop desc,topTime desc,id desc");
		InfoDaoImpl infoDao = InfoDaoImpl.getInstance();
		return infoDao.findVOByParamsPage(params, startNo, pageSize);
	}
	
	/**
	 * 根据站点查询信息
	 *	
	 * @param infoID 信息ID
	 * @param siteID 站点ID
	 * @return
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-11 下午09:38:04
	 * @version V1.0
	 */
	public Info getInfoBySite(long infoID, int siteID) throws Exception{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("parentID", infoID);
		params.put("siteID", siteID);
		params.put("state", 1);
		InfoDaoImpl infoDao = InfoDaoImpl.getInstance();
		return infoDao.findUniqueByParams(params);
	}
}
